﻿using System;
using System.Collections.Generic;
using JStore.Core.Entities;
using JStore.Data.Daos;
using SpeadFrm.Utility;

namespace JStore.Tasks
{
    public class WeekMoneyLogTask
    {
        private readonly WeekMoneyLogDao _weekMoneyLogDao = new WeekMoneyLogDao();
        private readonly MoneyLogDao _moneyLogDao = new MoneyLogDao();
        private readonly CompanyMoneyLogDao _companyMoneyLogDao = new CompanyMoneyLogDao();

        public List<WeekMoneyLog> GetAll()
        {
            return _weekMoneyLogDao.GetAll();
        }

        public List<WeekMoneyLog> GetList(int? weekMoneyLogId)
        {
            return _weekMoneyLogDao.GetList(weekMoneyLogId);
        }

        public void StatisticLastWeek()
        {
            var temp = DateTime.Now.AddDays(-7);
            var lastWeekMonday = DateTimeHelper.GetWeekMonday(temp);
            var lastWeekSunday = DateTimeHelper.GetWeenSunday(temp);

            if (_weekMoneyLogDao.Exists(lastWeekMonday, lastWeekSunday))
                return;
            var riseMoney = _moneyLogDao.GetSum(lastWeekMonday, lastWeekSunday);
            var realMoney = _companyMoneyLogDao.GetSum(lastWeekMonday, lastWeekSunday);

            var log = new WeekMoneyLog
                          {
                              CreateDate = DateTime.Now,
                              EndDate = lastWeekSunday,
                              StartDate = lastWeekMonday,
                              RealMoney = realMoney,
                              RiseMoney = riseMoney,
                          };
            _weekMoneyLogDao.Add(log);
        }
    }
}